<?php
/**
 * Created by PhpStorm.
 * User: pc
 * Date: 2019/1/14
 * Time: 20:18
 */

namespace app\admin\controller;

use think\Db;

class Workorder extends Main
{
    /**
     * 工单类型列表
     */
    public function category()
    {
        $data = Db::name('work_order_category')
            ->where('company_id', isession('company_id'))
            ->paginate(15);
        $this->assign('list', $data);
        return $this->fetch('category');
    }

    /**
     * 添加工单类型
     * @return mixed
     */
    public function cateAdd()
    {
        if (post()) {
            $data['category'] = trim(post('category'));
            $data['user_id'] = isession('user_id');
            $data['username'] = isession('username');
            $data['company_id'] = isession('company_id');
            $data['timestamp'] = time();
            Db::name('work_order_category')->insert($data);
            $this->success('添加成功');
        } else {
            return $this->fetch('cateAdd');
        }
    }

    /**
     * 编辑类型
     * @param $id
     * @return mixed
     * @throws \think\Exception
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     * @throws \think\exception\PDOException
     */
    public function cateEdit($id)
    {
        if (post()) {
            $data = post();
            unset($data['id']);
            DB::name('work_order_category')->where('id', post('id'))->update($data);
            $this->success("更新成功");
        } else {
            $info = DB::name('work_order_category')->where('id', $id)->find();
            $this->assign("info", $info);
            return $this->fetch('cateEdit');
        }
    }

    /**
     * 删除类型
     * @throws \think\Exception
     * @throws \think\exception\PDOException
     */
    public function cateDel()
    {
        $data = post();
        if (checkArr($data['id'])) {
            $where = "id in (" . join(",", $data['id']) . ")";
        } else {
            $where = "id =" . $data['id'];
        }
        Db::name('work_order_category')->where($where)->delete();
        $this->success("删除成功");
    }

    /**
     * 状态列表
     * @return mixed
     * @throws \think\exception\DbException
     */
    public function typeList()
    {
        $data = Db::name('work_order_state')
            ->where('company_id', isession('company_id'))
            ->paginate(15);
        $this->assign('list', $data);
        return $this->fetch('typeList');
    }

    /**
     * 添加远程数据
     * @return mixed
     */
    public function typeAdd()
    {
        if (post()) {
            $data['type'] = trim(post('type'));
            $data['user_id'] = isession('user_id');
            $data['username'] = isession('username');
            $data['company_id'] = isession('company_id');
            $data['timestamp'] = time();
            Db::name('work_order_state')->insert($data);
            $this->success('添加成功');
        } else {
            return $this->fetch('typeAdd');
        }
    }

    /**
     * 编辑
     * @param $id
     * @return mixed
     * @throws \think\Exception
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     * @throws \think\exception\PDOException
     */
    public function typeEdit($id)
    {
        if (post()) {
            $data = post();
            unset($data['id']);
            DB::name('work_order_state')->where('id', post('id'))->update($data);
            $this->success("更新成功");
        } else {
            $info = DB::name('work_order_state')->where('id', $id)->find();
            $this->assign("info", $info);
            return $this->fetch('typeEdit');
        }
    }

    /**
     * 删除
     * @throws \think\Exception
     * @throws \think\exception\PDOException
     */
    public function typeDel()
    {
        $data = post();
        if (checkArr($data['id'])) {
            $where = "id in (" . join(",", $data['id']) . ")";
        } else {
            $where = "id =" . $data['id'];
        }
        Db::name('work_order_state')->where($where)->delete();
        $this->success("删除成功");
    }

    /**
     * 我的工单
     */
    public function myWorkOrder()
    {
        $where = [];
        $where1 = '';
        $where['user_id'] = isession('user_id');
        Db::name('work_order')->where($where)->update(['send_popup' => 2]);

        get('order_number') && $where['order_number'] = ['like', '%' . get('order_number') . '%'];
        get('tracking_number') && $where['tracking_number'] = ['like', '%' . get('tracking_number') . '%'];
        get('reference_number') && $where['reference_number'] = ['like', '%' . get('reference_number') . '%'];
        get('category') && $where['category_id'] = get('category');
        get('state') && $where['state_id'] = get('state');
        get('status') && $where['status'] = get('status');

        if (get('start')) {
            $where1 .= "timestamp >=" . strtotime(get('start') . " 00:00:00");
        }
        if (get('end')) {
            $str = "timestamp <=" . strtotime(get('end') . " 23:59:59");
            $where1 .= empty($where1) ? $str : " AND " . $str;
        }

        if (get('overdue') == 1) {//过期
            $str = "deadline <=" . time();
            $where1 .= empty($where1) ? $str : " AND " . $str;
        } elseif (get('overdue') == 2) {//未过期
            $str = "deadline >=" . time();
            $where1 .= empty($where1) ? $str : " AND " . $str;
        }

        $beginToday = strtotime(date("Y-m-d") . " 00:00:00");
        $endToday = strtotime(date("Y-m-d") . " 23:59:59");
        if (get('advent') == 1) {//临期
            $str = "deadline <=" . $beginToday . " AND deadline>=" . $beginToday;
            $where1 .= empty($where1) ? $str : " AND " . $str;
        } elseif (get('advent') == 2) {//未临期
            $str = "deadline >=" . $endToday . " OR deadline<=" . $beginToday;
            $where1 .= empty($where1) ? $str : " AND " . $str;
        }
        $data = Db::name('work_order')
            ->where($where)
            ->where($where1)
            ->order('status asc,timestamp desc')
            ->paginate(15, false, ['query' =>
                [
                    'order_number' => get('order_number'),
                    'tracking_number' => get('tracking_number'),
                    'reference_number' => get('reference_number'),
                    'category' => get('category'),
                    'state' => get('state'),
                    'overdue' => get('overdue'),
                    'status' => get('status'),
                    'advent' => get('advent'),
                    'start' => get('start'),
                    'end' => get('end')
                ]]);
        //问题类型
        $this->assign('category', $this->getDictionary('work_order_category', 'company_id = ' . isession('company_id')));

        //工单处理状态
        $this->assign('state', $this->getDictionary('work_order_state', 'company_id = ' . isession('company_id')));

        $this->assign('list', $data);
        return $this->fetch('myWorkOrder');
    }

    /**
     * 添加工单
     * @return mixed
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function workOrderAdd()
    {
        if (post()) {
            $data = post();
            $insertData['title'] = $data['title'];
            $insertData['category_id'] = $data['category_id'];
            $insertData['category'] = $data['category'];
            $insertData['state_id'] = $data['state_id'];
            $insertData['state'] = $data['state'];
            $insertData['tracking_number'] = $data['tracking_number'];
            $insertData['reference_number'] = $data['reference_number'];
            $insertData['order_number'] = $data['order_number'];
            $insertData['logistics_number'] = $data['logistics_number'];
            $insertData['transport'] = $data['transport'];
            $insertData['deadline'] = strtotime($data['deadline'] . " 23:59:59");
            $insertData['commit'] = $data['commit'];
            $insertData['remark'] = $data['remark'];
            $insertData['user_id'] = isession('user_id');
            $insertData['company_id'] = isession('company_id');
            $insertData['timestamp'] = time();
            Db::name('work_order')->insert($insertData);
            $this->success('添加成功');
        } else {
            //问题类型
            $this->assign('category', $this->getDictionary('work_order_category', 'company_id = ' . isession('company_id')));

            //工单处理状态
            $this->assign('state', $this->getDictionary('work_order_state', 'company_id = ' . isession('company_id')));
            $this->assign('date', date('Y-m-d', time()));
            return $this->fetch('workOrderAdd');
        }
    }

    /**
     * 撤回工单
     * @param $id
     * @throws \think\Exception
     * @throws \think\exception\PDOException
     */
    public function workOrderRecall($id)
    {
        DB::name('work_order')->where('id', $id)->update(['status' => 3]);
        $this->success("撤回成功");
    }

    /**
     * 编辑工单
     */
    public function workOrderEdit($id)
    {
        if (post()) {
            $data['work_order_id'] = $id;
            $data['content'] = post('content');
            $data['user_id'] = isession('user_id');
            $data['username'] = ssession('username');
            $data['truename'] = ssession('truename');
            $data['timestamp'] = time();
            Db::name('work_order_reply')->insert($data);
            $sql = "UPDATE ean_work_order SET receive_num = (receive_num+" . 1 . "),receive_popup=1 WHERE id=" . $id;
            Db::execute($sql);
            $this->success("更新成功");
        } else {
            $info = DB::name('work_order')->where('id', $id)->find();
            $this->assign("info", $info);
            //获取对话详情
            $reply = DB::name('work_order_reply')->where('work_order_id', $id)->select();
            $this->assign("reply", $reply);
            //更改创建用户未读回复
            DB::name('work_order')->where('id', $id)->update(['send_num' => 0,'send_popup'=>2]);

            return $this->fetch('workOrderEdit');
        }
    }

    /**
     * 需处理工单
     * @return mixed
     * @throws \think\Exception
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     * @throws \think\exception\PDOException
     */
    public function workOrderDisposeList()
    {
        $where = [];
        $where1 = '';
        if (isession('type') == 2) {//子公司
            $where['company_id'] = isession('company_id');
            $where['commit'] = 1;
        } else {
            $where1 = "company_id = " . isession('company_id') . " OR commit=2";
        }
        $where['status'] = ['neq', 3];
        $where['user_id'] = ['neq', isession('user_id')];
        Db::name('work_order')->where($where)->where($where1)->update(['receive_popup' => 2]);

        get('order_number') && $where['order_number'] = ['like', '%' . get('order_number') . '%'];
        get('tracking_number') && $where['tracking_number'] = ['like', '%' . get('tracking_number') . '%'];
        get('reference_number') && $where['reference_number'] = ['like', '%' . get('reference_number') . '%'];
        get('status') && $where['status'] = get('status');

        if (get('start')) {
            $where1 .= "timestamp >=" . strtotime(get('start') . " 00:00:00");
        }
        if (get('end')) {
            $str = "timestamp <=" . strtotime(get('end') . " 23:59:59");
            $where1 .= empty($where1) ? $str : " AND " . $str;
        }

        if (get('overdue') == 1) {//过期
            $str = "deadline <=" . time();
            $where1 .= empty($where1) ? $str : " AND " . $str;
        } elseif (get('overdue') == 2) {//未过期
            $str = "deadline >=" . time();
            $where1 .= empty($where1) ? $str : " AND " . $str;
        }

        $beginToday = strtotime(date("Y-m-d") . " 00:00:00");
        $endToday = strtotime(date("Y-m-d") . " 23:59:59");
        if (get('advent') == 1) {//临期
            $str = "deadline <=" . $beginToday . " AND deadline>=" . $beginToday;
            $where1 .= empty($where1) ? $str : " AND " . $str;
        } elseif (get('advent') == 2) {//未临期
            $str = "deadline >=" . $endToday . " OR deadline<=" . $beginToday;
            $where1 .= empty($where1) ? $str : " AND " . $str;
        }

        $data = Db::name('work_order')
            ->where($where)
            ->where($where1)
            ->order('status asc,timestamp desc')
            ->paginate(15, false, ['query' =>
                [
                    'order_number' => get('order_number'),
                    'tracking_number' => get('tracking_number'),
                    'reference_number' => get('reference_number'),
                    'overdue' => get('overdue'),
                    'advent' => get('advent'),
                    'status' => get('status'),
                    'start' => get('start'),
                    'end' => get('end')
                ]]);
        //问题类型
        $this->assign('category', $this->getDictionary('work_order_category', 'company_id = ' . isession('company_id')));

        //工单处理状态
        $this->assign('state', $this->getDictionary('work_order_state', 'company_id = ' . isession('company_id')));

        $this->assign('list', $data);
        return $this->fetch('workOrderDisposeList');
    }

    /**
     * 处理工单
     * @param $id
     * @return mixed
     * @throws \think\Exception
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     * @throws \think\exception\PDOException
     */
    public function workOrderReply($id)
    {
        if (post()) {
            $data = post();
            $insertData['work_order_id'] = $id;
            $insertData['content'] = $data['content'];
            $insertData['user_id'] = isession('user_id');
            $insertData['username'] = ssession('username');
            $insertData['truename'] = ssession('truename');
            $insertData['timestamp'] = time();
            Db::name('work_order_reply')->insert($insertData);
            $str = isset($data['status']) ? ",status=4" : ",status=2";
            $sql = "UPDATE ean_work_order SET send_num = (send_num+" . 1 . ")" . $str . ",send_popup=1 WHERE id=" . $id;
            Db::execute($sql);
            $this->success("更新成功");
        } else {
            $info = DB::name('work_order')->where('id', $id)->find();
            $this->assign("info", $info);
            //获取对话详情
            $reply = DB::name('work_order_reply')->where('work_order_id', $id)->select();
            $this->assign("reply", $reply);
            //更改创建用户未读回复
            DB::name('work_order')->where('id', $id)->update(['receive_num' => 0,'receive_popup'=>2]);

            return $this->fetch('workOrderReply');
        }
    }

    /**
     * 批量完成工单状态
     * @throws \think\Exception
     * @throws \think\exception\PDOException
     */
    public function workOrderDispose()
    {
        $data = post();
        if (checkArr($data['id'])) {
            $where = "id in (" . join(",", $data['id']) . ")";
        } else {
            $where = "id =" . $data['id'];
        }
        Db::name('work_order')->where($where)->update(['status' => 4]);
        $this->success("更新成功");
    }

}